﻿using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebApi333.Data;
using WebApi333.Data.Entity;
using WebApi333.Domain;

namespace WebApi333.Utils
{
    public class DbInitializer
    {
        public static void Seed(IServiceProvider serviceProvider)
        {
            using (var scope = serviceProvider.CreateScope())
            {
                var db = scope.ServiceProvider.GetService(typeof(Admin2222DbContext)) as Admin2222DbContext;

                var _userRespository = scope.ServiceProvider.GetService(typeof(Irespository<Users>)) as EfRespository<Users>;
                var _RolesRespository = scope.ServiceProvider.GetService(typeof(Irespository<Roles>)) as EfRespository<Roles>;

                db.Database.EnsureCreated();

                var hasuser = db.Users.Any();

                if (!hasuser)
                {
                    var user = new Users()
                    {
                        UserName = "abc",
                        PassWord = "123"
                    };
                    _userRespository.Insert(user);
                    var role = new Roles()
                    {
                        RoleName = "asnfuoaofao"
                    };
                    _RolesRespository.Insert(role);
                }
            }
        }
    }
}
